package com.example.soft.demos.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PaperList {
    private final String teacher;
    private final List<Paper> papers;
    private final PaperSelection selections;
    private PaperSelectionStrategy strategy;

    public PaperList(String teacher, List<Paper> papers, PaperSelectionStrategy strategy) {
        this.teacher = teacher;
        this.papers = papers;
        this.selections = new PaperSelection();
        this.strategy = strategy;
    }

    public void setStrategy(PaperSelectionStrategy strategy) {
        this.strategy = strategy;
    }

    // 获取未被选择的指定类型论文
    public List<Paper> getUnselectedPapersByType(PaperType type) {
        List<Paper> result = new ArrayList<>();
        for (Paper paper : papers) {
            if (!selections.isSelected(paper) &&
                    (type == PaperType.Any || isPaperTypeMatch(paper, type))) {
                result.add(paper);
            }
        }
        return result;
    }

    // 判断论文类型是否匹配
//    private boolean isPaperTypeMatch(Paper paper, PaperType type) {
//        if (paper instanceof Thesis && type == PaperType.Thesis) return true;
//        if (paper instanceof JournalPaper && type == PaperType.JournalPaper) return true;
//        if (paper instanceof ConferencePaper && type == PaperType.ConferencePaper) return true;
//        return false;
//    }

    // 随机选择论文（含异常处理）
    public List<Paper> selectPaperByRandom(Student student) throws NoEnoughPapersException {
        if (student == null) {
            throw new IllegalArgumentException("学生对象不可为空");
        }
        if (hasSelectedPapers(student)) {
            throw new IllegalArgumentException("学生已选择过论文");
        }
        if (student.getPreferredQuantity() < 1) {
            throw new IllegalArgumentException("偏好数量需≥1");
        }

        List<Paper> availablePapers = getUnselectedPapersByType(student.getPreferredPaperType());
        int availableCount = availablePapers.size();

        if (availableCount < student.getPreferredQuantity()) {
            throw new NoEnoughPapersException(
                    student.getName(),
                    student.getPreferredQuantity(),
                    availableCount
            );
        }

        List<Paper> selectedPapers = strategy.selectPapers(availablePapers, student.getPreferredQuantity());
        for (Paper paper : selectedPapers) {
            selections.addSelection(student, paper);
        }
        return selectedPapers;
    }

    // 检查学生是否已选论文
    private boolean hasSelectedPapers(Student student) {
        for (Student s : selections.getSelections().values()) {
            if (s.getName().equals(student.getName())) {
                return true;
            }
        }
        return false;
    }

    // 获取选择结果（防御性拷贝，避免表示泄露）
    public PaperSelection getSelectionResult() {
        PaperSelection result = new PaperSelection();
        for (Map.Entry<Paper, Student> entry : selections.getSelections().entrySet()) {
            result.addSelection(entry.getValue(), entry.getKey());
        }
        return result;
    }
    private boolean isPaperTypeMatch(Paper paper, PaperType type) {
        if (paper instanceof Thesis && type == PaperType.Thesis) return true;
        if (paper instanceof JournalPaper && type == PaperType.JournalPaper) return true;
        if (paper instanceof ConferencePaper && type == PaperType.ConferencePaper) return true;
        return false;
    }
}